package com.jxc.summer.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.jxc.summer.service.CableService;
import com.jxc.summer.service.ColorService;
import com.jxc.summer.service.CustomerService;
import com.jxc.summer.service.InOrderService;
import com.jxc.summer.service.QualityService;
import com.jxc.summer.service.UnitService;
import com.jxc.summer.service.WareHouseService;
import com.jxc.summer.util.JacksonUtil;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

@Controller
@RequestMapping("/inorder")
public class InOrderController {
	
	@Autowired
	private CustomerService customerService;
	@Autowired
	private CableService cableService;
	@Autowired 
	private UnitService unitService;
	@Autowired
	private ColorService colorService;
	@Autowired
	private QualityService qualityService;
	@Autowired
	private InOrderService inOrderService;
	@Autowired
	private WareHouseService wareHouseService;
	
	@RequestMapping("/inorderin")
	public String index(Model model,@SessionAttribute("accountCompanyCode") String companyCode) {
		//获取账户下的客户
		model.addAttribute("customerList",customerService.getAllList(companyCode));
		model.addAttribute("orderid",UUIDUtil.getOrderIdByUUId());
		model.addAttribute("cableModelList", cableService.getModel(companyCode));
		model.addAttribute("unitList", unitService.getUnit(companyCode));
		model.addAttribute("colorList", colorService.getColor(companyCode));
		model.addAttribute("qualityList", qualityService.getQuality(companyCode));
		model.addAttribute("warehouseList", wareHouseService.get(companyCode));
		//获取账户下的线缆型号
		return "inorderin/inorderin.add";
	}
	@RequestMapping("/inorderinAdd")
	@ResponseBody
	public ReturnT<String> inorderinAdd(@SessionAttribute("accountCompanyCode") String companyCode,HttpServletRequest request){
		String orderid = request.getParameter("orderid");
		String customerid = request.getParameter("customerid");
		String customername = request.getParameter("customername");
		String contacts = request.getParameter("contacts");
		String telephone = request.getParameter("telephone");
		String address = request.getParameter("address");
		String warehouse = request.getParameter("warehouse");
		/*String mywareHouse = request.getParameter("mywareHouse");*/
		String remark = request.getParameter("remark");
		String countZong = request.getParameter("countZong");
		String order = request.getParameter("orderList");
		List<Map<String,Object>> orderList = JacksonUtil.readValue(order, List.class);
		Map<String,Object> params= new HashMap<String,Object>();
		params.put("to_companycode", companyCode);
		params.put("to_orderid", orderid);
		params.put("to_customerid", customerid);
		params.put("to_customername", customername);
		params.put("to_contacts", contacts);
		params.put("to_telephone", telephone);
		params.put("to_address", address);
		params.put("to_warehouse", warehouse);
		/*params.put("mywareHouse", mywareHouse);*/
		params.put("to_ordertime", new Date());
		params.put("to_remark", remark);
		params.put("to_totalprice", countZong);
		params.put("orderList", orderList);
		try{
			inOrderService.addin(params);
		}catch (Exception e) {
			return new ReturnT<String>(500, "入库失败！");
		}
		return ReturnT.SUCCESS;
	}
}
